package com.wangcang.widgetlibrary.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.TextView;

import com.wangcang.widgetlibrary.R;
import com.wangcang.widgetlibrary.activity.base.BaseActivity;
import com.wangcang.widgetlibrary.adapter.CityAdapter;
import com.wangcang.widgetlibrary.adapter.TagAdapter;
import com.wangcang.widgetlibrary.bean.CityBean;
import com.wangcang.widgetlibrary.bean.ItemBean;
import com.wangcang.widgetlibrary.bean.SectionBean;
import com.wangcang.widgetlibrary.bean.TagBean;
import com.wangcang.widgetlibrary.decoration.TitleItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class MultiSelectActivity extends BaseActivity {

	private RecyclerView mRvRight;
	private List<CityBean> mCityData;
	private List<TagBean> mTagList;
	private CityAdapter mRightAdapter;
	private RecyclerView mRvLeft;
	private TagAdapter mLeftAdapter;
	private LinearLayoutManager mRvRightLayout;
	private List<SectionBean> mSectionList;
	private TextView mTvCount;
	private boolean isFromLefrRvClick;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_multi_select);
		mRvRight = (RecyclerView) findViewById(R.id.recycler_view_right);
		mRvLeft = (RecyclerView) findViewById(R.id.rv_left);
		mTvCount = (TextView) findViewById(R.id.tv_count);
		mRvRight.setLayoutManager(mRvRightLayout= new LinearLayoutManager(this));
		mRvLeft.setLayoutManager(new LinearLayoutManager(this));
		initData();
		mRvRight.setAdapter(mRightAdapter = new CityAdapter(mCityData));
		mRightAdapter.setOnItemClickListener(new CityAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(CityBean cityBean, int position) {
				cityBean.setSelected(!cityBean.isSelected());
//				for (TagBean tagBean : mTagList) {
//					int count = 0;
//					for (CityBean bean : mCityData) {
//						if (TextUtils.equals(tagBean.getTag(), bean.getTag())) {
//							count++;
//						}
//					}
//					tagBean.setItemCount(count);
//				}
				for (TagBean tagBean : mTagList) {
					if (TextUtils.equals(tagBean.getTag(), cityBean.getTag())) {
						if (cityBean.isSelected()) {
							tagBean.plus();
						} else {
							tagBean.cut();
						}
						break;
					}
				}
				mLeftAdapter.notifyDataSetChanged();
				mRightAdapter.notifyDataSetChanged();
				mTvCount.setText(String.format("已选(%d)", getCount()));
			}
		});
		mRvRight.addItemDecoration(new TitleItemDecoration(this, mCityData));
		mLeftAdapter = new TagAdapter(mTagList);
		mLeftAdapter.setOnItemClickListener(new TagAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(TagBean tagBean, int position) {

				isFromLefrRvClick = true;
				tagBean.setSelected(true);
				mLeftAdapter.notifyDataSetChanged();
//				int sum = 0;
				int scrollPosition = 0;
//				for (int i = 1; i < mCityData.size(); i++) {
//					CityBean prevCityBean = mCityData.get(i - 1);
//					CityBean currentCityBean = mCityData.get(i);
//					if (!TextUtils.equals(prevCityBean.getTag(), currentCityBean.getTag())) {
//						sum++;
//					}
//					if (sum == position) {
//						scrollPosition = i;
//						break;
//					}
//				}
				for (int i = 0; i < mCityData.size(); i++) {
					if (TextUtils.equals(tagBean.getTag(), mCityData.get(i).getTag())) {
						scrollPosition = i;
						break;
					}
				}
				mRvRightLayout.scrollToPositionWithOffset(scrollPosition, 0);
			}
		});
		mRvLeft.setAdapter(mLeftAdapter);
		mRvRight.addOnScrollListener(new RecyclerView.OnScrollListener() {

			@Override
			public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
				if (isFromLefrRvClick) {
					isFromLefrRvClick = false;
					return;
				}
				int firstVisibleItemPosition = mRvRightLayout.findFirstVisibleItemPosition();
				for (TagBean tagBean : mTagList) {
					if (TextUtils.equals(mCityData.get(firstVisibleItemPosition).getTag(), tagBean.getTag())) {
						//	将上次选中的选项重置为非选中状态
						mLeftAdapter.restore();
						tagBean.setSelected(true);
						mLeftAdapter.notifyDataSetChanged();
						break;
					}
				}
			}
		});

		mTvCount.setText(String.format("已选(%d)", getCount()));

	}

	private int getCount() {
		int count = 0;
		for (TagBean tagBean : mTagList) {
			count += tagBean.getItemCount();
		}
		return count;

	}

	private void initData() {
		mCityData = new ArrayList<>();
		mCityData.add(new CityBean("A", "安徽"));
		mCityData.add(new CityBean("B", "北京"));
		mCityData.add(new CityBean("F", "福建"));
		mCityData.add(new CityBean("G", "广东"));
		mCityData.add(new CityBean("G", "甘肃"));
		mCityData.add(new CityBean("G", "贵州"));
		mCityData.add(new CityBean("G", "广西"));
		mCityData.add(new CityBean("H", "河南"));
		mCityData.add(new CityBean("H", "湖北"));
		mCityData.add(new CityBean("H", "湖南"));
		mCityData.add(new CityBean("H", "河北"));
		mCityData.add(new CityBean("J", "江苏"));
		mCityData.add(new CityBean("J", "江苏"));
		mCityData.add(new CityBean("J", "江苏"));
		mCityData.add(new CityBean("J", "江苏"));
		mCityData.add(new CityBean("J", "江苏"));
		mCityData.add(new CityBean("J", "江苏"));
		mCityData.add(new CityBean("J", "江苏"));
		mCityData.add(new CityBean("J", "江苏"));
		mCityData.add(new CityBean("J", "江苏"));
		mCityData.add(new CityBean("J", "江苏"));
		mCityData.add(new CityBean("J", "江苏"));
		mCityData.add(new CityBean("J", "江苏"));
		mCityData.add(new CityBean("J", "江苏"));
		mCityData.add(new CityBean("J", "江苏"));
		mCityData.add(new CityBean("J", "江苏"));
		mCityData.add(new CityBean("R", "日本"));
		mCityData.add(new CityBean("R", "日本"));
		mCityData.add(new CityBean("R", "日本"));
		mCityData.add(new CityBean("R", "日本"));

		mTagList = new ArrayList<>();
		mTagList.add(new TagBean("A", false));
		mTagList.add(new TagBean("B", false));
		mTagList.add(new TagBean("F", false));
		mTagList.add(new TagBean("G", false));
		mTagList.add(new TagBean("H", false));
		mTagList.add(new TagBean("J", false));
		mTagList.add(new TagBean("R", false));

		mSectionList = new ArrayList<>();

		List<ItemBean> list = new ArrayList<>();
		list.add(new ItemBean("安徽", false));
		mSectionList.add(new SectionBean(getItemList(list), "A"));

		list.add(new ItemBean("北京", false));
		mSectionList.add(new SectionBean(getItemList(list), "B"));

		list.add(new ItemBean("福建", false));
		mSectionList.add(new SectionBean(getItemList(list), "F"));

		list.add(new ItemBean("广东", false));
		list.add(new ItemBean("湖北", false));
		list.add(new ItemBean("贵州", false));
		list.add(new ItemBean("广西", false));
		mSectionList.add(new SectionBean(getItemList(list), "G"));

		list.add(new ItemBean("河南", false));
		list.add(new ItemBean("湖北", false));
		list.add(new ItemBean("湖南", false));
		list.add(new ItemBean("河北", false));
		mSectionList.add(new SectionBean(getItemList(list), "H"));

		list.add(new ItemBean("江苏", false));
		list.add(new ItemBean("江苏", false));
		list.add(new ItemBean("江苏", false));
		list.add(new ItemBean("江苏", false));
		list.add(new ItemBean("江苏", false));
		list.add(new ItemBean("江苏", false));
		list.add(new ItemBean("江苏", false));
		list.add(new ItemBean("江苏", false));
		list.add(new ItemBean("江苏", false));
		list.add(new ItemBean("江苏", false));
		list.add(new ItemBean("江苏", false));
		list.add(new ItemBean("江苏", false));
		list.add(new ItemBean("江苏", false));
		list.add(new ItemBean("江苏", false));
		list.add(new ItemBean("江苏", false));
		mSectionList.add(new SectionBean(getItemList(list), "J"));

		list.add(new ItemBean("日本", false));
		list.add(new ItemBean("日本", false));
		list.add(new ItemBean("日本", false));
		list.add(new ItemBean("日本", false));
		mSectionList.add(new SectionBean(getItemList(list), "R"));

	}

	private SectionBean getSection(String tag) {
		List<ItemBean> itemBeanList = new ArrayList<>();

		return new SectionBean(itemBeanList, tag);
	}

	private List<ItemBean> getItemList(List<ItemBean> list) {
		List<ItemBean> itemBeanList = new ArrayList<>();
		itemBeanList.addAll(list);
		list.clear();
		return itemBeanList;
	}
}
